#!/usr/bin/env python
# -*- coding: utf-8 -*-
# Time       :   14:57
# Author     : Y4
# Site       : 
# File       : backend_movie_view.py
import os

from flask import Blueprint, request

from App.common.backend.valify_backend_user import valify_login
from validator import validate, Required

from App.common.utils.util import rerep, single_dict, filename_transfer, allowed_file, multi_dict
from App.models.backend import AdminMovie

adminMovie = Blueprint('adminMovie', __name__, url_prefix='/admin/movie')

# 参数效验
'''
    moviename = models.Column(models.String(64))                # 电影名称
    enmoviename = models.Column(models.String(128))             # 显示英文名字
    director = models.Column(models.String(64))                 # 导演
    leadingRole = models.Column(models.String(256))             # 主演
    type = models.Column(models.String(64))                     # 电影类型 军事-恐怖
    country = models.Column(models.String(64))                  # 国家
    language = models.Column(models.String(64))                 # 语言
    duraction = models.Column(models.Integer, default=90)       # 时长
    screeningmodel = models.Column(models.String(32))           # 荧屏类型 3D 2D
    openday = models.Column(models.DateTime)                    # 播放日期
    backgroundimg = models.Column(models.String(256))           # 电影封面图
    flag = models.Column(models.Boolean, default=False)         # 标记 (推荐)
    is_delete = models.Column(models.Boolean, default=False)    # 逻辑删除
'''
rules = {
    "moviename": [Required],
    "enmoviename": [Required],
    "director": [Required],
    "leadingRole": [Required],
    "type": [Required],
    "country": [Required],
    "language": [Required],
    "duraction": [Required],
    "screeningmodel": [Required],
    "openday": [Required],
    "backgroundimg": [Required],
}


@adminMovie.route('/')
@valify_login
def index():
    movies = AdminMovie.query.all()
    print(movies)
    return rerep(data=multi_dict(movies))


@adminMovie.route('/add', methods=['POST'])
def add_movie():
    data = {}
    data['moviename'] = request.form.get("moviename")
    data['enmoviename'] = request.form.get("enmoviename")
    data['director'] = request.form.get("director")
    data['leadingRole'] = request.form.get("leadingRole")
    data['type'] = request.form.get("type")
    data['country'] = request.form.get("country")
    data['language'] = request.form.get("language")
    data['duraction'] = request.form.get("duraction")
    data['screeningmodel'] = request.form.get("screeningmodel")
    data['openday'] = request.form.get("openday")
    data['backgroundimg'] = request.files.get("backgroundimg")
    valid, errors = validate(rules, data)
    if not valid:
        return rerep(msg="参数错误", status=400)

    adminMovie = AdminMovie()
    adminMovie.moviename = request.form.get("moviename")
    adminMovie.enmoviename = request.form.get("enmoviename")
    adminMovie.director = request.form.get("director")
    adminMovie.leadingRole = request.form.get("leadingRole")
    adminMovie.type = request.form.get("type")
    adminMovie.country = request.form.get("country")
    adminMovie.language = request.form.get("language")
    adminMovie.duraction = request.form.get("duraction")
    adminMovie.screeningmodel = request.form.get("screeningmodel")
    adminMovie.openday = request.form.get("openday")

    backgroundimg = request.files.get("backgroundimg")
    # 验证图片格式
    if not (backgroundimg and allowed_file(backgroundimg.filename)):
        return rerep(msg="请检查上传的图片类型，仅限于png、PNG、jpg、JPG、bmp", status=400)
    # 图片修改名称
    fileinfo = filename_transfer(backgroundimg.filename)
    filepath = fileinfo[0]
    backgroundimg.save(filepath)  # 保存本地路径
    adminMovie.backgroundimg = fileinfo[1]  # 保存数据库路径

    if not adminMovie.save():
        return rerep(msg="添加电影失败", status=500)
    return rerep(msg="添加电影成功", status=200, data=single_dict(adminMovie))


@adminMovie.route('/upload', methods=['POST'])
def upload_img():
    backgroundimg = request.files['backgroundimg']
    # 检查图片格式
    if not (backgroundimg and allowed_file(backgroundimg.filename)):
        return rerep(msg="请检查上传的图片类型，仅限于png、PNG、jpg、JPG、bmp", status=400)
    # 修改图片名称,保存本地,保存数据库
    fileinfo = filename_transfer(backgroundimg.filename)
    filepath = fileinfo[0]
    backgroundimg.save(filepath)  # 保存本地路径

    # adminMovie.backgroundimg = fileinfo[1]  # 保存数据库路径
    return rerep(msg="上传成功", status=200)
